package com.sservice.banfu.controller;

import com.sservice.banfu.e.SaleInfo;
import com.sservice.banfu.e.SalePerson;
import com.sservice.banfu.mapper.SaleInfoMapper;
import com.sservice.banfu.service.SalePersonService1;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sservice.banfu.vo.SalePersonVo;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.base.utils.service.SService;
import com.sservice.commonBusiness.e.PLetter;
import com.sservice.commonBusiness.service.PLetterService;
import com.sservice.commonBusiness.vo.PLetterVo;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Controller("salePersonController1")
@RequestMapping("/admin/salePerson1/")
public class SalePersonController {

    @Autowired
    SalePersonService1 salePersonService1;
    @Autowired
    SaleInfoMapper saleInfoMapper;
    @Autowired
    PLetterService pLetterService;


    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(SalePerson salePerson, HttpSession session) {
        if ("4".equals(salePerson.getType1())) {
            SalePerson salePerson1 = (SalePerson) session.getAttribute(SService.CURRENT_SP);
            salePerson.setStatus("" + salePerson1.getId());
        }
        return new BaseResult(true, String.valueOf(salePersonService1.save(salePerson)), 200);
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult delete(@RequestParam Integer id) {
        return new BaseResult(true, String.valueOf(salePersonService1.deleteById(id)), 200);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(SalePersonVo salePerson) {
        SalePerson salePerson1 = new SalePerson();
        salePerson1.setPassword(salePerson.getPassword());
        salePerson1.setPhone(salePerson.getPhone());
        salePerson1.setEmail(salePerson.getEmail());
        salePerson1.setAvatar(salePerson.getAvatar());
        salePerson1.setCheckStatus(salePerson.getCheckStatus());
        salePerson1.setFavor(salePerson.getFavor());
        salePerson1.setId(salePerson.getId());
        salePerson1.setName(salePerson.getName());
        salePerson1.setSalesCount(salePerson.getSalesCount());
        salePerson1.setShowNameUserName(salePerson.getShowNameUserName());
        salePerson1.setValid(salePerson.getValid());
        salePerson1.setUserName(salePerson.getUserName());
        salePerson1.setStatus(salePerson.getStatus());
        //salePerson1.setType1(salePerson.getType1());
        SaleInfo saleInfo = new SaleInfo();
        saleInfo.setSalePersonId(String.valueOf(salePerson.getId()));
        saleInfo.setOpenid(salePerson.getOpenid());
        saleInfo.setBankAccount(salePerson.getBankAccount());
        saleInfo.setBankName(salePerson.getBankName());
        saleInfo.setProfile(salePerson.getProfile());
        saleInfo.setCustomerCount(salePerson.getCustomerCount());
        saleInfo.setIdCard(salePerson.getIdCard());
        saleInfo.setIdCardFront(salePerson.getIdCardFront());
        saleInfo.setIdCardReverse(salePerson.getIdCardReverse());
        saleInfo.setZhifubao(salePerson.getZhifubao());
        saleInfo.setWxNum(salePerson.getWxNum());
        saleInfo.setWeibo(salePerson.getWeibo());
        saleInfo.setTags(salePerson.getTags());
        int t = salePersonService1.update(salePerson1);
        SaleInfo saleInfo1 = new SaleInfo();
        saleInfo1.setSalePersonId(String.valueOf(salePerson.getId()));

        SaleInfo saleInfo2 = saleInfoMapper.selectOne(saleInfo1);
        if (saleInfo2 == null) {
            saleInfoMapper.insertSelective(saleInfo);
        } else {
            saleInfo.setId(saleInfo2.getId());
            saleInfoMapper.updateByPrimaryKeySelective(saleInfo);
        }
        return new BaseResult(true, String.valueOf(t), 200);
    }

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(SalePerson salePerson) {
        return new BaseResult(true, salePersonService1.detail(salePerson));
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SalePerson> list = salePersonService1.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return new ListResult(true, pageInfo);
    }

    @RequestMapping("query")
    @ResponseBody
    public ListResult query(SalePersonVo salePersonVo) {
        List<SalePersonVo> list = salePersonService1.page(salePersonVo);
        ListResult lr = new ListResult(true, list);
        Pagination pagination = new Pagination(salePersonVo.getPage(), salePersonService1.count(salePersonVo), salePersonVo.getSize());
        lr.setPage(pagination);
        return lr;
    }
    @RequestMapping("teacher")
    @ResponseBody
    public ListResult teacher(SalePersonVo salePersonVo) {
        return salePersonService1.queryTeacher(salePersonVo);
    }

    /**
     * 增加评论
     * @param pLetter
     * @param session
     * @return
     */
    @RequestMapping(value = "comment", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(PLetter pLetter, HttpSession session) {
        SalePerson salePerson = (SalePerson) session.getAttribute(SService.CURRENT_SP);
        if (salePerson != null) {
            pLetter.setFromUser(String.valueOf(salePerson.getId()));
        }
        return new BaseResult(true, String.valueOf(pLetterService.insertSelective(pLetter)), 200);
    }
    @RequestMapping("letter")
    @ResponseBody
    public ListResult list(PLetterVo page, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(SService.CURRENT_SP);
        page.setToUser(String.valueOf(sp.getId()));
        return pLetterService.page(page);
    }
}
